<!DOCTYPE html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/scripting_controller.html">
<link rel="import" href="/tracing/extras/tquery/filter.html">

<script>
'use strict';

tr.exportTo('tr.e.tquery', function() {
  function FilterHasAncestor(opt_subExpression) {
    this.subExpression = opt_subExpression;
  }

  FilterHasAncestor.prototype = {
    __proto__: tr.e.tquery.Filter.prototype,

    set subExpression(expr) {
      this.subExpression_ = tr.e.tquery.Filter.normalizeFilterExpression(expr);
    },

    get subExpression() {
      return this.subExpression_;
    },

    evaluate(context) {
      if (!this.subExpression) {
        return context.ancestors.length > 0;
      }
      while (context.ancestors.length) {
        context = context.pop();
        if (this.subExpression.evaluate(context)) return true;
      }
      return false;
    }
  };
  tr.c.ScriptingObjectRegistry.register(
      function(subExpression) {
        return new FilterHasAncestor(subExpression);
      },
      {
        name: 'hasAncestor'
      }
  );
  return {
    FilterHasAncestor,
  };
});
</script>
